package com.zhc.dynamic;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.*;
import com.baomidou.dynamic.datasource.creator.basic.BasicDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.beecp.BeeCpDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.dbcp.Dbcp2DataSourceCreator;
import com.baomidou.dynamic.datasource.creator.druid.DruidDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.jndi.JndiDataSourceCreator;
import com.zhc.domain.DataSourceDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.Set;

@RestController
@RequestMapping("/datasources")
public class DataSourceController {

    @Autowired
    private DataSource dataSource;
    // private final DataSourceCreator dataSourceCreator; //3.3.1及以下版本使用这个通用，强烈推荐sb2用户至少升级到3.5.2版本

    @Autowired
    private DefaultDataSourceCreator dataSourceCreator;
    //如果是用4.x以上版本，因为要和spring解绑，重构了一些东西，比如缺少了懒启动和启动初始化数据库。不太建议用以下独立的创建器，只建议用上面的DefaultDataSourceCreator
//    @Autowired
//    private BasicDataSourceCreator basicDataSourceCreator;
//    @Autowired
//    private JndiDataSourceCreator jndiDataSourceCreator;
//    @Autowired
//    private DruidDataSourceCreator druidDataSourceCreator;

    @GetMapping
    // ("获取当前所有数据源")
    public Set<String> now() {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        return ds.getDataSources().keySet();
    }

    //通用数据源会根据maven中配置的连接池根据顺序依次选择。
    //默认的顺序为druid>hikaricp>beecp>dbcp>spring basic
    @PostMapping("/add")
    // ("通用添加数据源（推荐）")
    public Set<String> add(@Validated @RequestBody DataSourceDTO dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPoolName(), dataSource);
        return ds.getDataSources().keySet();
    }
//
//    @PostMapping("/addBasic(强烈不推荐，除了用了马上移除)")
//    // (value = "添加基础数据源", notes = "调用Springboot内置方法创建数据源，兼容1,2")
//    public Set<String> addBasic(@Validated @RequestBody DataSourceDTO dto) {
//        DataSourceProperty dataSourceProperty = new DataSourceProperty();
//        BeanUtils.copyProperties(dto, dataSourceProperty);
//        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
//        DataSource dataSource = basicDataSourceCreator.createDataSource(dataSourceProperty);
//        ds.addDataSource(dto.getPoolName(), dataSource);
//        return ds.getDataSources().keySet();
//    }
//
//    @PostMapping("/addJndi")
//    // ("添加JNDI数据源")
//    public Set<String> addJndi(String poolName, String jndiName) {
//        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
//        DataSource dataSource = jndiDataSourceCreator.createDataSource(jndiName);
//        ds.addDataSource(poolName, dataSource);
//        return ds.getDataSources().keySet();
//    }
//
//    @PostMapping("/addDruid")
//    // ("基础Druid数据源")
//    public Set<String> addDruid(@Validated @RequestBody DataSourceDTO dto) {
//        DataSourceProperty dataSourceProperty = new DataSourceProperty();
//        BeanUtils.copyProperties(dto, dataSourceProperty);
//        dataSourceProperty.setLazy(true);
//        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
//        DataSource dataSource = druidDataSourceCreator.createDataSource(dataSourceProperty);
//        ds.addDataSource(dto.getPoolName(), dataSource);
//        return ds.getDataSources().keySet();
//    }



    @DeleteMapping
    // ("删除数据源")
    public String remove(String name) {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        ds.removeDataSource(name);
        return "删除成功";
    }
}